package com.starlink.controller.wechat;

import com.github.pagehelper.PageInfo;
import com.lop.open.api.sdk.domain.ECAP.CommonQueryOrderApi.commonGetOrderTraceV1.CommonOrderTraceDetail;
import com.lop.open.api.sdk.domain.ECAP.CommonQueryOrderApi.commonGetOrderTraceV1.CommonOrderTraceResponse;
import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.starlink.common.annotation.client.weixin.UserPurchaseRes;
import com.starlink.common.annotation.client.weixin.WeixinJsRefundOrderRes;
import com.starlink.model.request.mall.*;
import com.starlink.model.vo.book.MallArticlesBuyVO;
import com.starlink.model.vo.mall.MallOrderDetailListVO;
import com.starlink.model.vo.mall.MallOrderListVO;
import com.starlink.model.vo.mall.WechatRefundsVO;
import com.starlink.model.vo.mall.WeixinRefundOrderVO;
import com.starlink.service.MallOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.starlink.common.enums.OperationTypeEnum.USER_DAI_FA_HUO_PRICE;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/4/9 14:00
 * @Description: WechatBookOrderController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/wechat/bookorder")
@Api(value = "【小程序端】商城订单模块", tags = {"【小程序端】商城订单模块"})
public class WechatOrderController {

    @Autowired
    private MallOrderService bookOrderService;

    @WebAccess(login = true)
    @ApiOperation("单商品及购物车确认订单页信息")
    @PostMapping("/selectBuy")
    public WebResponse<MallArticlesBuyVO> selectBuy(@RequestBody MallBuyRequest request) {
        return WebResponse.build(bookOrderService.selectBuy(request));
    }

    @WebAccess(login = true)
    @ApiOperation("单商品及购物车库存校验")
    @PostMapping("/checkInventory")
    public WebResponse<MallArticlesBuyVO> checkInventory (@RequestBody MallBuyRequest request) {
        return WebResponse.build(bookOrderService.checkInventory(request));
    }

    @WebAccess(login = true)
    @ApiOperation("运费计算")
    @PostMapping("/calculateFreight")
    public WebResponse<MallArticlesBuyVO> calculateFreight (@RequestBody MallBuyRequest request) {
        return WebResponse.build(bookOrderService.calculateFreight(request));
    }

    @WebAccess(login = true)
    @ApiOperation("预支付并生成订单")
    @PostMapping("/payment")
    public WebResponse<UserPurchaseRes> payment(@RequestBody MallBuyRequest request) {
        return WebResponse.build(bookOrderService.payment(request));
    }

    @WebAccess(login = true)
    @ApiOperation("商城订单查询")
    @PostMapping("/selectMallOrderList")
    public WebResponse<PageInfo<List<MallOrderListVO>>> selectMallOrderList(@SortDefault(sort = {"create_time"}, direction = Sort.Direction.DESC) Pageable pageable,
                                                                            @RequestBody MallOrderListRequest request) {
        return WebResponse.build(bookOrderService.selectMallOrderList(request,pageable));
    }

    @WebAccess(login = true)
    @ApiOperation("查询订单详情")
    @PostMapping("/selectOrderDetail")
    public WebResponse<MallOrderDetailListVO> selectOrderDetail(@RequestBody MallCancelOrderRequest request) {
        return WebResponse.build(bookOrderService.selectOrderDetail(request));
    }

    @WebAccess(login = true)
    @ApiOperation("待付款状态下的取消订单")
    @PostMapping("/cancelOrder_waitPay")
    public WebResponse<Boolean> cancelOrderWaitPay(@RequestBody MallCancelOrderRequest request) {
        return WebResponse.build(bookOrderService.cancelOrderWaitPay(request));
    }

    @WebAccess(login = true)
    @ApiOperation("取消已付款待发货订单")
    @PostMapping("/cancelOrder_isPay")
    public WebResponse<WeixinRefundOrderVO> cancelOrderIsPay(@RequestBody MallCancelOrderRequest request) {
        WechatRefund wechatRefund = new WechatRefund();
        wechatRefund.setOperationType(USER_DAI_FA_HUO_PRICE.getType());
        return WebResponse.build(bookOrderService.cancelOrderIsPay(request,wechatRefund));
    }

    @WebAccess(login = true)
    @ApiOperation("查询售后中订单详情")
    @PostMapping("/selectRefundOrderDetail")
    public WebResponse<WechatRefundsVO> selectRefundOrderDetail(@RequestBody MallCancelOrderRequest request) {
        return WebResponse.build(bookOrderService.selectRefundOrderDetail(request));
    }

    @WebAccess(login = true)
    @ApiOperation("订单轨迹查询")
    @PostMapping("/traceQuery")
    public WebResponse<List<CommonOrderTraceDetail>> traceQuery(@RequestBody MallOrderConfirmRequest request) {
        return WebResponse.build(bookOrderService.traceQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("确认收货")
    @PostMapping("/confirmGoods")
    public WebResponse<Boolean> confirmGoods(@RequestBody MallOrderConfirmRequest request) {
        return WebResponse.build(bookOrderService.confirmGoods(request));
    }
}
